﻿using QLBH.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLBH.DATA
{
    public class KhachHangData
    {
        private static QLBHEntities db = new QLBHEntities();
        public string Error { set; get; }

        public List<KHACH_HANG> TimKhachHangTheoTen(string ten_khach_hang, int tong_so_dong)
        {
            db = new QLBHEntities();
            List<KHACH_HANG> list = new List<KHACH_HANG>();
            if (ten_khach_hang == "")
            {
                list = db.KHACH_HANG.Where(p => p.DA_XOA == false).ToList().Skip(tong_so_dong).Take(20).ToList().Select(p => new KHACH_HANG()
                    {
                        DA_XOA = p.DA_XOA,
                        DIA_CHI = p.DIA_CHI,
                        DIEN_THOAI = p.DIEN_THOAI,
                        EMAIL=p.EMAIL,
                        FAX=p.FAX,
                        LAN_SUA_CUOI=p.LAN_SUA_CUOI,
                        MA_KHACH_HANG=p.MA_KHACH_HANG,
                        MO_TA = p.MO_TA,
                        NGAY_SINH=p.NGAY_SINH,
                        NGUOI_SUA=p.NGUOI_SUA,
                        TAI_KHOAN_NGAN_HANG=p.TAI_KHOAN_NGAN_HANG,
                        TEN_KHACH_HANG = p.TEN_KHACH_HANG,
                        WEBSITE = p.WEBSITE
                    }).ToList();
            }
            else
                list = db.KHACH_HANG.Where(p => p.DA_XOA == false && p.TEN_KHACH_HANG.Contains(ten_khach_hang)).ToList().Skip(tong_so_dong).Take(20).ToList().Select(p => new KHACH_HANG()
                {
                    DA_XOA = p.DA_XOA,
                    DIA_CHI = p.DIA_CHI,
                    DIEN_THOAI = p.DIEN_THOAI,
                    EMAIL = p.EMAIL,
                    FAX = p.FAX,
                    LAN_SUA_CUOI = p.LAN_SUA_CUOI,
                    MA_KHACH_HANG = p.MA_KHACH_HANG,
                    MO_TA = p.MO_TA,
                    NGAY_SINH = p.NGAY_SINH,
                    NGUOI_SUA = p.NGUOI_SUA,
                    TAI_KHOAN_NGAN_HANG = p.TAI_KHOAN_NGAN_HANG,
                    TEN_KHACH_HANG = p.TEN_KHACH_HANG,
                    WEBSITE = p.WEBSITE
                }).ToList(); ;
            return list;
        }


        private string TaoMa()
        {
            string result = "";
            if (db.KHACH_HANG.ToList().Count <= 0)
            {
                result = "0001";
            }
            else
            {
                int maxId = 0;

                foreach (var item in db.KHACH_HANG.ToList())
                {
                    if (maxId < int.Parse(item.MA_KHACH_HANG.Substring(2)))
                    {
                        maxId = int.Parse(item.MA_KHACH_HANG.Substring(2));
                    }
                }
                switch (maxId.ToString().Length)
                {
                    case 1:
                        result = "000" + (maxId + 1);
                        break;


                    case 2:
                        result = "00" + (maxId + 1);
                        break;


                    case 3:
                        result = "0" + (maxId + 1);
                        break;


                    case 4:
                        result = "" + (maxId + 1);
                        break;
                    default:
                        break;
                }
            }
            return "KH" + result;
        }

        public bool ThemMoi(KHACHHANGPARAM KHACHHANG)
        {
            try
            {
                db = new QLBHEntities();
                KHACH_HANG pb = new KHACH_HANG();
                pb.MA_KHACH_HANG = KHACHHANG.MA_KHACH_HANG;
                pb.TEN_KHACH_HANG = KHACHHANG.TEN_KHACH_HANG;
                pb.DIA_CHI = KHACHHANG.DIA_CHI;
                pb.DIEN_THOAI = KHACHHANG.DIEN_THOAI;
                pb.EMAIL = KHACHHANG.EMAIL;
                pb.WEBSITE = KHACHHANG.WEBSITE;
                pb.TAI_KHOAN_NGAN_HANG = KHACHHANG.TAI_KHOAN_NGAN_HANG;
                pb.FAX = KHACHHANG.FAX;
                pb.MO_TA = KHACHHANG.MO_TA;
                pb.NGAY_SINH = KHACHHANG.NGAY_SINH;
                pb.DA_XOA = false;
                pb.LAN_SUA_CUOI = DateTime.Now;
                pb.NGUOI_SUA = KHACHHANG.NGUOI_SUA;

                if (pb.MA_KHACH_HANG.Trim().Length <= 0)
                {
                    pb.MA_KHACH_HANG = TaoMa();
                }
                else if (db.KHACH_HANG.Any(p => p.MA_KHACH_HANG == pb.MA_KHACH_HANG))
                {
                    Error = "Mã khách hàng đã tồn tại";
                    return false;
                }

                db.KHACH_HANG.Add(pb);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message;
                return false;
            }
        }

        public bool CapNhat(KHACHHANGPARAM KHACHHANG)
        {
            try
            {
                db = new QLBHEntities();
                KHACH_HANG n = db.KHACH_HANG.Where(p => p.MA_KHACH_HANG == KHACHHANG.MA_KHACH_HANG).FirstOrDefault();
                if (n == null)
                {
                    Error = "khách hàng không tồn tại";
                    return false;
                }

                if (n.LAN_SUA_CUOI.ToString("ddMMyyyyhhmmss") != KHACHHANG.LAN_SUA_CUOI.ToString("ddMMyyyyhhmmss"))
                {
                    Error = "khách hàng đã được sửa bởi " + n.NGUOI_SUA + " Vào lúc : " + n.LAN_SUA_CUOI.ToString("dd/MM/yyyy hh:mm:ss");
                    return false;
                }

                n.TEN_KHACH_HANG = KHACHHANG.TEN_KHACH_HANG;
                n.DIA_CHI = KHACHHANG.DIA_CHI;
                n.DIEN_THOAI = KHACHHANG.DIEN_THOAI;
                n.EMAIL = KHACHHANG.EMAIL;
                n.WEBSITE = KHACHHANG.WEBSITE;
                n.TAI_KHOAN_NGAN_HANG = KHACHHANG.TAI_KHOAN_NGAN_HANG;
                n.FAX = KHACHHANG.FAX;
                n.MO_TA = KHACHHANG.MO_TA;
                n.DA_XOA = false;
                n.NGAY_SINH = KHACHHANG.NGAY_SINH;
                n.LAN_SUA_CUOI = DateTime.Now;
                n.NGUOI_SUA = KHACHHANG.NGUOI_SUA;

                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }
        }

        public bool Xoa(string[] MA_KHACH_HANG)
        {
            try
            {
                db = new QLBHEntities();
                for (int i = 0; i < MA_KHACH_HANG.Length; i++)
                {
                    string m = MA_KHACH_HANG[i];
                    KHACH_HANG n = db.KHACH_HANG.Where(p => p.MA_KHACH_HANG == m).FirstOrDefault();
                    if (n == null)
                    {
                        Error = "khách hàng không tồn tại";
                        return false;
                    }

                    n.DA_XOA = true;
                    db.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }
        }

    }
}